/*
	       File: PollingController.cs
	Create Date: 2007-08-01
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public class PollingController
    {
        #region Private member fields
        private int _PollingInterval;
        private Timer _PollingTimer;
        private PollingCallback _Callback;
        #endregion

        #region Constructors and cleanup methods
        private PollingController()
        {
        }
        public PollingController(int pollingSeconds, PollingCallback callback)
        {
            _PollingInterval = pollingSeconds * 1000; //convert seconds to ms
            _Callback = callback;

            _PollingTimer = new Timer();
            _PollingTimer.Elapsed += new ElapsedEventHandler(this.TimerIntervalElapsed);
            _PollingTimer.Interval = _PollingInterval;
            _PollingTimer.Enabled = false;
        }
        #endregion

        #region Private implementation methods
        private void TimerIntervalElapsed(object o, ElapsedEventArgs e)
        {
            Logger.LogEnter();
            _PollingTimer.Enabled = false; //stop the timer
            if (null != _Callback)
            {
                _Callback.Execute();
            }
            Logger.LogLeave();
        }
        #endregion

        #region Events and delegates
        #endregion

        #region Public interface
        public void StartPollingInterval()
        {
            _PollingTimer.Enabled = true;
        }
        #endregion
    }
}
